# ################################################################ #
# c.lib.make                                                       #
#                                                                  #
# Common makefile for C libraries                                  #
#                                                                  #
# (c) 2002-2003 Wouter Caarls, Delft University of Technology      #
# ################################################################ #

# Variables ###################################################### #

SOURCES = $(shell ls *.c *.C *.cc *.cpp 2>/dev/null)
OBJECTS = $(patsubst %.c, %.o, $(patsubst %.C, %.o, $(patsubst %.cc, %.o, $(patsubst %.cpp, %.o, $(SOURCES)))))
MAKEFILE_DEPS = Makefile $(SHARE)/vars.make $(SHARE)/include.make $(SHARE)/c.lib.make $(SHARE)/c.depend

# Compilation #################################################### #

$(filter-out $(OVERRIDE_TARGETS), all): subdirs.all $(NAME)

ifndef NO_STATIC_LIB
$(NAME): $(NAME).a
endif
ifndef NO_SHARED_LIB
$(NAME): $(NAME).so.$(MAJOR).$(MINOR).$(REVISION) $(NAME).so.$(MAJOR) $(NAME).so ;
endif

$(filter-out $(OVERRIDE_TARGETS), $(NAME).a): $(OBJECTS)
	$(AR) rcs $@ $(OBJECTS)

$(filter-out $(OVERRIDE_TARGETS), $(NAME).so.$(MAJOR).$(MINOR).$(REVISION)): $(OBJECTS)
	$(LINKER) -shared -Wl,-soname,$(NAME).so.$(MAJOR) -o $@ $(OBJECTS) $(LFLAGS)

$(filter-out $(OVERRIDE_TARGETS), $(NAME).so.$(MAJOR)): $(NAME).so.$(MAJOR).$(MINOR).$(REVISION)
	ln -sf $@.$(MINOR).$(REVISION) $@

$(filter-out $(OVERRIDE_TARGETS), $(NAME).so): $(NAME).so.$(MAJOR).$(MINOR).$(REVISION)
	ln -sf $@.$(MAJOR).$(MINOR).$(REVISION) $@

$(filter-out $(OVERRIDE_TARGETS), %.o):: %.c $(MAKEFILE_DEPS)
	$(CC) $(CFLAGS) -fPIC -c $<
$(filter-out $(OVERRIDE_TARGETS), %.o):: %.C $(MAKEFILE_DEPS)
	$(CPP) $(CFLAGS) -fPIC -c $<
$(filter-out $(OVERRIDE_TARGETS), %.o):: %.cc $(MAKEFILE_DEPS)
	$(CPP) $(CFLAGS) -fPIC -c $<
$(filter-out $(OVERRIDE_TARGETS), %.o):: %.cpp $(MAKEFILE_DEPS)
	$(CPP) $(CFLAGS) -fPIC -c $<

# Documentation ################################################## #

$(filter-out $(OVERRIDE_TARGETS), doc): force
	cd doc ; $(DOXYGEN) doxygen.conf
	if [ -s doc/latex ]; then $(MAKE) -C doc/latex ps; fi

# Administration ################################################# #

$(filter-out $(OVERRIDE_TARGETS), clean): subdirs.clean
	rm -rf core core.[0-9]* $(NAME).a $(NAME).so* *~ *.o *.d *.i doc/*~ doc/html doc/latex $(NAME)-$(MAJOR).$(MINOR).$(REVISION) $(NAME)-$(MAJOR).$(MINOR).$(REVISION).tar.gz $(NAME)-$(MAJOR).$(MINOR).$(REVISION).tar

$(filter-out $(OVERRIDE_TARGETS), install): subdirs.install $(NAME) $(BINARIES) $(HEADERS)
ifndef NO_INSTALL
	install -d $(PREFIX)/lib
ifndef NO_STATIC_LIB
	install -pc $(NAME).a $(PREFIX)/lib
endif
ifndef NO_SHARED_LIB
	install -pc $(NAME).so.$(MAJOR).$(MINOR).$(REVISION) $(PREFIX)/lib
	ln -sf $(PREFIX)/lib/$(NAME).so.$(MAJOR).$(MINOR).$(REVISION) $(PREFIX)/lib/$(NAME).so.$(MAJOR)
	ln -sf $(PREFIX)/lib/$(NAME).so.$(MAJOR).$(MINOR).$(REVISION) $(PREFIX)/lib/$(NAME).so
endif
ifdef HEADERS
	install -d $(PREFIX)/include
	install -pc -m 644 $(HEADERS) $(PREFIX)/include
endif
ifdef BINARIES
	install -d $(PREFIX)/bin
	install -pc $(BINARIES) $(PREFIX)/bin
endif
endif

$(filter-out $(OVERRIDE_TARGETS), uninstall): subdirs.uninstall
ifndef NO_INSTALL
	rm -f $(PREFIX)/lib/$(NAME).a $(PREFIX)/lib/$(NAME).so.$(MAJOR).$(MINOR).$(REVISION) $(PREFIX)/lib/$(NAME).so.$(MAJOR) $(PREFIX)/lib/$(NAME).so
ifdef HEADERS
	rm -f $(HEADERS:%=$(PREFIX)/include/%)
endif
ifdef BINARIES
	rm -f $(BINARIES:%=$(PREFIX)/bin/%)
endif
endif

# Distribution ################################################### #

$(filter-out $(OVERRIDE_TARGETS), subdist): clean subdirs.subdist c.lib.subdist ;

$(filter-out $(OVERRIDE_TARGETS), c.lib.subdist): force
	mkdir -p $(DISTDIR)
	cp `find . -maxdepth 1 -name "*.[cCh]" -o -name "*.cc" -o -name "*.cpp"` $(DISTDIR)
	cp -f $(MAKEFILE_DEPS) $(DISTDIR)/$(SHAREDISTDIR)
ifdef DISTFILES
	cp --parent $(DISTFILES) $(DISTDIR)
endif
ifdef HEADERS
	cp --parent $(HEADERS) $(DISTDIR)
endif
ifdef BINARIES
	cp --parent $(BINARIES) $(DISTDIR)
endif
	sed -e "s/.*SHARE *=.*/SHARE = $(SEDSHAREDISTDIR)/" Makefile > $(DISTDIR)/Makefile
	if [ -s doc ]; then cp -R doc $(DISTDIR); fi

# Dependencies ################################################### #

$(filter-out $(OVERRIDE_TARGETS), %.d):: %.c $(MAKEFILE_DEPS)
	$(SHARE)/c.depend $(CC) $(CFLAGS) $*
$(filter-out $(OVERRIDE_TARGETS), %.d):: %.C $(MAKEFILE_DEPS)
	$(SHARE)/c.depend $(CPP) $(CFLAGS) $*
$(filter-out $(OVERRIDE_TARGETS), %.d):: %.cc $(MAKEFILE_DEPS)
	$(SHARE)/c.depend $(CPP) $(CFLAGS) $*
$(filter-out $(OVERRIDE_TARGETS), %.d):: %.cpp $(MAKEFILE_DEPS)
	$(SHARE)/c.depend $(CPP) $(CFLAGS) $*

ifeq ($(filter clean uninstall dist subdist undist distclean realclean, $(MAKECMDGOALS)),)
-include $(filter %.d, $(patsubst %.c, %.d, $(patsubst %.C, %.d, $(patsubst %.cc, %.d, $(patsubst %.cpp, %.d, $(SOURCES))))))
endif
